package cn.shop.front.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.shop.base.MyServiceImpl;
import cn.shop.front.entity.gamebind.GameBind;
import cn.shop.front.entity.gamebind.MerchantAlias;
import cn.shop.front.mapper.GameBindMapper;
import cn.shop.front.service.GameBindService;
import cn.shop.front.util.AliasUtil;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2022-2-14 10:24
 * @description :
 */
@Service
public class GameBindServiceImpl extends MyServiceImpl<GameBindMapper, GameBind> implements GameBindService {

	@Resource
	private HttpServletRequest request;

	@Override
	public List<Map<String, Object>> bindList(long uid) {
		List<GameBind> list = baseMapper.bindList(uid);
		list.forEach(g -> g.setAccount(StrUtil.split(g.getAccount(), "э").get(0)));
		list = list.stream().sorted(Comparator.comparing(GameBind::getAccount)).collect(Collectors.toList());
		List<GameBind> binds = new ArrayList<>();
		List<String> checkList = new ArrayList<>();
		for (GameBind gb : list) {
			String checkStr = gb.getPartitionId() + gb.getAccount() + gb.getNickname();
			if (!checkList.contains(checkStr)) {
				binds.add(gb);
				checkList.add(checkStr);
			}
		}
		return binds.stream().map(this::format).collect(Collectors.toList());
	}

	@Override
	public List<Map<String, Object>> partitionBinds(long userId, long partitionId) {
		List<GameBind> list = list(Wrappers.<GameBind>lambdaQuery()
				.eq(GameBind::getUserId, userId)
				.eq(GameBind::getPartitionId, partitionId));
		return list.stream()
				.map(gb -> Putif.start()
						.anyway("value", gb.getId())
						.anyway("label", gb.getNickname())
						.get())
				.collect(Collectors.toList());
	}

	@Override
	public List<Map<String, Object>> allGame() {
		String domain = AliasUtil.getAlias(request);
		if (StrUtil.isNotBlank(domain)) {
			return baseMapper.allGame(domain);
		}
		return Collections.emptyList();

	}

	@Override
	public List<Map<String, Object>> allGame(long uid) {
		return baseMapper.allGameByUid(uid);
	}

	@Override
	public List<Map<String, Object>> allPartitionByGame(long gameId) {
		return baseMapper.allPartitionByGame(gameId);
	}

	@Override
	public Long getGameId(long partitionId) {
		return baseMapper.getGameIdByPartitionId(partitionId);
	}

	@Override
	public Map<String, Object> getGame(long partitionId) {
		return baseMapper.getGameByPartitionId(partitionId);
	}

	@Override
	public MerchantAlias checkAlias(String name) {
		return baseMapper.checkAlias(name);
	}

	@Override
	public Long checkLogin(long uid, LocalDateTime time) {
		return baseMapper.queryPartitonTime(uid, time);
	}

	@Override
	public List<Map<String, Object>> userGame(long userId) {
		return baseMapper.userGame(userId);
	}

	private Map<String, Object> format(GameBind bind) {
		String account = "";
		List<String> temp = StrUtil.split(bind.getAccount(), "э");
		if (temp != null && temp.size() > 0) {
			account = temp.get(0);
		}
		return Putif.start()
				.anyway("id", bind.getId())
//				.anyway("url", bind.getAlias() + ".haojy.com")
				.anyway("url", bind.getAlias() + ".162pay.com")
				.anyway("partitionName", bind.getPartitionName())
				.anyway("gameName", bind.getGameName())
				.anyway("account", account)
				.anyway("nickname", bind.getNickname())
				.get();
	}

}
